import { 头盔, 手枪, 防弹衣, 突击步枪, 冲锋枪, 射手步枪, 栓动步枪, 轻机枪, 霰弹枪, 卡宾枪, 投掷物, 子弹, 药品, 食物, ItemData, } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import AudioManager from "../Framework/Managers/AudioManager";
import { BundleManager, Bundles } from "../Framework/Managers/BundleManager";
import { DataManager } from "../Framework/Managers/DataManager";
import { EventManager, MyEvent } from "../Framework/Managers/EventManager";
import { PoolManager } from "../Framework/Managers/PoolManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import { ResourceUtil } from "../Framework/Utils/ResourceUtil";
import { ZTool } from "../Framework/Utils/ZTool";
import ValueBar from "./ValueBar";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemDetailPanel extends cc.Component {

    TitleLabel: cc.Label = null;
    DescLabel: cc.Label = null;
    PriceLabel: cc.Label = null;
    Icon: cc.Sprite = null;
    ValuesContent: cc.Node = null;

    valueBars: cc.Node[] = [];

    protected onLoad(): void {
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.TitleLabel = NodeUtil.GetComponent("TitleLabel", this.node, cc.Label);
        this.DescLabel = NodeUtil.GetComponent("DescLabel", this.node, cc.Label);
        this.PriceLabel = NodeUtil.GetComponent("PriceLabel", this.node, cc.Label);
        this.ValuesContent = NodeUtil.GetNode("ValuesContent", this.node);
    }

    Show(data: ItemData) {
        this.TitleLabel.string = `${data.Name}`;
        this.PriceLabel.string = `参考单价：${data.Price}`;
        this.DescLabel.string = data.hasOwnProperty("Desc") ? `${(data as any).Desc}` : ``;

        let scale = 2;
        if (data.Type == ItemType[ItemType.头盔]) {
            scale = 1.6;
            this.DescLabel.string = `防护等级：${(data as any).Lv}`;
        }
        if (data.Type == ItemType[ItemType.防弹衣]) {
            scale = 1.8;
            this.DescLabel.string = `防护等级：${(data as any).Lv}`;
        }
        if (data.Type == ItemType[ItemType.子弹]) {
            scale = 1.6;
            this.DescLabel.string = `${(data as any).Name}`;
        }

        this.Icon.node.setScale(scale);

        this.RefreshValues(data);

        BundleManager.LoadSpriteFrame(Bundles.Item, `${data.Type}_${data.Name}`).then((sf: cc.SpriteFrame) => {
            this.Icon.spriteFrame = sf;
        });

    }

    RefreshValues(data) {
        let hasValues = false;
        this.valueBars.forEach(e => PoolManager.PutNode(e));
        let type = ItemType[`${data.Type}`];
        if (ItemData.IsGun(type)) {
            hasValues = true;
            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init(data.Damage / 90, `伤害`);
                this.valueBars.push(node);
            });
            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init((1.5 - data.FireRate) / 1.5, `射速`);
                this.valueBars.push(node);
            });
            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init(data.Recoil / 0.5, `后坐力`);
                this.valueBars.push(node);
            });
        }

        if (ItemData.IsEquip(type)) {
            hasValues = true;

            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init(data.Lv / 5, `防护等级`);
                this.valueBars.push(node);
            });
            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init(data.Durability / 100, `耐久度`);
                this.valueBars.push(node);
            });
        }

        if (ItemData.IsMedicine(type)) {
            hasValues = true;

            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init(data.HP / 500, `血量`);
                this.valueBars.push(node);
            });
            PoolManager.GetNode("Prefabs/UI/ValueBar", this.ValuesContent).then((node: cc.Node) => {
                node.getComponent(ValueBar).Init((6 - data.Time) / 6, `速度`);
                this.valueBars.push(node);
            });
        }

        this.Icon.node.setPosition(hasValues ? -200 : 0, 0);
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);

        switch (event.target.name) {
            case "Mask":
            case "CloseButton":
                UIManager.Instance.HidePanel(Constant.Panel.ItemDetailPanel);
                break;
        }
    }
}
